package com.matt.service.impl;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.vo.CompleteRateVo;
import com.matt.foundation.vo.IconTargetVo;
import com.matt.foundation.vo.StatisticsVo;
import com.matt.repository.base.StatisticsNativeRepository;
import com.matt.remoteCall.SsoCallService;
import com.matt.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

/**
 * ***********************
 * Created by Ron
 * from com.matt
 * on 2018/6/29 0029.
 * ***********************
 */
@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private StatisticsNativeRepository statisticsNativeRepository;


    @Override
    public StatisticsVo findNewRecords(String areaId, String beginDate, String endDate) throws ParseException {
        StatisticsVo statisticsVo = statisticsNativeRepository.findNewRecords(areaId, beginDate, endDate);
        return statisticsVo;
    }

    @Override
    public List<IconTargetVo> countRecord(List<StaffDTO> staffDTOList, String areaId, String beginDate, String endDate) {
        //查询园区的所有员工人员staffId、staffName
        if (staffDTOList.size() > 0) {
            //查询园区工作人员的薪资排名
            List<IconTargetVo> iconTargetVos = statisticsNativeRepository.getFiverStaffInfo(areaId, beginDate, endDate);
            for (IconTargetVo iconTargetVo : iconTargetVos) {
                for (int j = 0; j < staffDTOList.size(); j++) {
                    if (iconTargetVo.getStaffId().equals(staffDTOList.get(j).getId())) {
                        iconTargetVo.setStaffName(staffDTOList.get(j).getRealName());
                    }
                }
            }
            return iconTargetVos;
        }
        return null;
    }

    @Override
    public CompleteRateVo completeRate(String staffId, String areaId, String date) {
        CompleteRateVo vo = new CompleteRateVo();
        //查询目标业绩
        BigDecimal aimPerformance = statisticsNativeRepository.aimPerformance(staffId, areaId, date);
        vo.setAimPerformance(aimPerformance);

        //查询实际业绩
        BigDecimal actualPerformance = statisticsNativeRepository.actualPerformance(staffId, areaId, date);
        vo.setActualPerformance(actualPerformance);
        return vo;
    }
}
